import * as iiotServices from '@/services/iiot';

export default {
  namespace: 'fakemodel',
  state: {
    eseProject: null,
    eseProjects: [],
    loadedProjects: false,
    loadedDevices: false,
    eseDevices: [],
    eseDevicde: {},
    loadedOmOrders: false,
    eseOmOrders: [],
    loadedRtDatas: false,
    eseRtDatas: [],
    loadedServices: false,
    eseDCServices: [],
    loadedRangRtDatas: false,
    eseRangRtDatas: [],
    loadedDailyLogs: false,
    eseDailyLogs: [],
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const responseProject = yield call(iiotServices.queryEseProjects, payload);
      yield put({
        type: 'queryProjects',
        payload: Array.isArray(responseProject) ? responseProject : [],
      });
    },

    *fetchProject({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryEseProject, payload);

      yield put({
        type: 'queryProject',
        payload: response,
      });
    },

    *fetchDevices({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryEseDevices, payload);
      yield put({
        type: 'queryDevices',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *fetchOmOrders({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryOmOrders, payload);
      yield put({
        type: 'queryOmOrders',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *fetchDailyLogs({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryDailyLogs, payload);
      yield put({
        type: 'queryDailyLogs',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *fetchRtDatas({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryRtDatas, payload);
      yield put({
        type: 'queryRtDatas',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *fetchiiotServices({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryEseDcServices, payload);
      yield put({
        type: 'queryEseDcServices',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *fetchRangeRtDatas({ payload }, { call, put }) {
      const response = yield call(iiotServices.queryRangRtDatas, { point_code: payload });
      console.log(response);
      yield put({
        type: 'queryRangRtDatas',
        payload: Array.isArray(response) ? response : [],
      });
    },

  },

  reducers: {
    queryProject(state, action) {

      return {
        ...state,
        loadedProjects: true,
        eseProject: action.payload,
      };
    },
    queryProjects(state, action) {

      return {
        ...state,
        loadedProjects: true,
        eseProjects: action.payload,
      };
    },
    queryDevices(state, action) {

      return {
        ...state,
        loadedDevices: true,
        eseDevices: action.payload,
      };
    },
    queryOmOrders(state, action) {

      return {
        ...state,
        loadedOmOrders: true,
        eseOmOrders: action.payload,
      };
    },
    queryRtDatas(state, action) {
      return {
        ...state,
        loadedRtDatas: true,
        eseRtDatas: action.payload,
      };
    }
    ,

    queryRangRtDatas(state, action) {
      return {
        ...state,
        loadedRangRtDatas: true,
        eseRangRtDatas: action.payload,
      };
    }
    ,


    queryEseDcServices(state, action) {
      return {
        ...state,
        loadedServices: true,
        eseDCServices: action.payload,
      };
    },

    queryDailyLogs(state, action) {
      return {
        ...state,
        loadedDailyLogs: true,
        eseDailyLogs: action.payload,
      };
    },



  }

};

